package com.tuwan.sensorcatcher

import android.Manifest
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.PowerManager
import android.view.View
import android.widget.EditText
import android.widget.Toast
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
import android.os.Build
import android.support.v4.app.ActivityCompat


class MainActivity : AppCompatActivity() {

    private  val mPeriodEdit: EditText by lazy<EditText>{
         findViewById<EditText>(R.id.et_period)
    }
    private val mWakeLock : PowerManager.WakeLock by lazy<PowerManager.WakeLock> {
        ( getSystemService(Context.POWER_SERVICE) as  PowerManager ).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CPU保持运行");// CPU保存运行
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        checkPermission()
    }


    fun start(v: View){
        var intent = Intent(this,CatcherService::class.java);
        if(mPeriodEdit.text!=null){
            mWakeLock.acquire()
            intent.putExtra("set_period",Integer.valueOf(mPeriodEdit.text.toString()))
            startService(intent)
            showToast("服务已开启")
        }else{
            showToast("请先设置采样周期!")
        }
    }

    fun stop(v: View){
        var intent = Intent(this,CatcherService::class.java)
        stopService(intent)
        mWakeLock.release()
        showToast("服务已关闭")
    }

    private val REQUEST_CODE_ASK_PERMISSIONS = 123
    fun checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            val permissionStrs = ArrayList<String>()
            val hasWriteSdcardPermission = ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
            if (hasWriteSdcardPermission != PackageManager.PERMISSION_GRANTED) {
                permissionStrs.add(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            }
            val stringArray = permissionStrs.toTypedArray()
            if (permissionStrs.size > 0) {
                ActivityCompat.requestPermissions(this,
                        stringArray,REQUEST_CODE_ASK_PERMISSIONS)
                return
            }
        }
    }

    //权限设置后的回调函数，判断相应设置，requestPermissions传入的参数为几个权限，则permissions和grantResults为对应权限和设置结果
     override fun onRequestPermissionsResult(requestCode: Int,
                                   permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CODE_ASK_PERMISSIONS ->
                //可以遍历每个权限设置情况
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //这里写你需要相关权限的操作
                } else {
                    Toast.makeText(this,
                            "权限没有开启", Toast.LENGTH_SHORT).show()
                }
        }
        super.onRequestPermissionsResult(requestCode, permissions,
                grantResults)
    }

}


fun MainActivity.showToast(str:String){

    Toast.makeText(this,str,Toast.LENGTH_SHORT).show()
}




